package cheme.ProcessEditor.Policies;

import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy;
import org.eclipse.gef.requests.CreateConnectionRequest;
import org.eclipse.gef.requests.ReconnectRequest;

import cheme.ProcessEditor.Figures.Util.ChemePolylineDecoration;
import cheme.ProcessModel.Cmds.CreateStreamCmd;
import cheme.ProcessModel.Cmds.ReconnectStreamCmd;
import cheme.ProcessModel.ProcessUnits.IProcessUnit;
import cheme.ProcessModel.ProcessUnits.ProcessUnit;
import cheme.ProcessModel.ProcessUnits.Streams.AbstractStream;


public class ChemeGraphicalNodePolicy extends GraphicalNodeEditPolicy
{
	int originalDepth = -1;
	int currentDepth = originalDepth;
	
//	@Override
//	public void showTargetFeedback(Request request)
//	{	
//		try
//		{
//			if(getHost().isActive() && currentDepth == originalDepth)
//			{
//				System.out.println("showTargetFeedback");
//				
//				IFigure parent = getHostFigure().getParent();
//				IFigure child = getHostFigure();
//				
//				originalDepth = parent.getChildren().indexOf(child);
//				
//				parent.remove(child);
//				parent.add(child, -1);
//				currentDepth = 0;
//			}
//		}
//		catch(Exception e)
//		{
//			e.printStackTrace();
//		}
//		
//		super.showTargetFeedback(request);
//	}
	
//	@Override
//	public void eraseTargetFeedback(Request request)
//	{
//		try
//		{
//			if(currentDepth != originalDepth)
//			{
//				System.out.println("eraseTargetFeedback");
//				
//				IFigure parent = getHostFigure().getParent();
//				IFigure child = getHostFigure();
//				
//				parent.remove(child);
//				parent.add(child, originalDepth);
//				
//				currentDepth = originalDepth;
//			}
//		}
//		catch(Exception e)
//		{
//			e.printStackTrace();
//		}
//		
//		super.eraseTargetFeedback(request);
//	}
	
	protected Command getConnectionCompleteCommand(CreateConnectionRequest request)
	{
		CreateStreamCmd cmd = null;
		
		if(getHost().getModel() instanceof IProcessUnit)
		{
			ProcessUnit pu = (ProcessUnit)getHost().getModel();
			if(pu.willAcceptInStream())
			{
				cmd = (CreateStreamCmd)request.getStartCommand();
				if(cmd.getSource() != pu)
				{
					cmd.setSink(pu);
				}
				else
				{
					cmd = null;
				}
			}
		}
		
		return cmd;
	}

	protected Command getConnectionCreateCommand(CreateConnectionRequest request)
	{
		CreateStreamCmd cmd = null;
		
		if(getHost().getModel() instanceof ProcessUnit)
		{
			ProcessUnit pu = (ProcessUnit)getHost().getModel();
//			IStream stream = new Stream(pu);
			
			if(pu.willAcceptOutStream())
			{
				cmd = new CreateStreamCmd(pu);
//				cmd.setStream(stream);
				request.setStartCommand(cmd);
			}
		}
		
		return cmd;
	}

	protected Command getReconnectTargetCommand(ReconnectRequest request)
	{
		ReconnectStreamCmd cmd = null;
		
		if(getHost().getModel() instanceof ProcessUnit)
		{
			ProcessUnit pu = (ProcessUnit)getHost().getModel();
			if(pu.willAcceptInStream())
			{
				cmd = new ReconnectStreamCmd();
				cmd.setStream((AbstractStream)request.getConnectionEditPart().getModel());
				cmd.setNewSink(pu);
			}
		}
		
		return cmd;
	}

	protected Command getReconnectSourceCommand(ReconnectRequest request)
	{
		ReconnectStreamCmd cmd = null;
		
		if(getHost().getModel() instanceof ProcessUnit)
		{
			ProcessUnit pu = (ProcessUnit)getHost().getModel();
			if(pu.willAcceptOutStream())
			{
				cmd = new ReconnectStreamCmd();
				cmd.setStream((AbstractStream)request.getConnectionEditPart().getModel());
				cmd.setNewSource(pu);
			}
		}
		
		return cmd;
	}
	
	protected Connection createDummyConnection(Request req)
	{
		PolylineConnection connection = new PolylineConnection();
		
		ChemePolylineDecoration decorator = new ChemePolylineDecoration();
		decorator.setScale(12, 6);
		connection.setTargetDecoration(decorator);
		
		return connection;
	}
}
